package org.Lin.controller;

import org.Lin.service.ChatRoomMessageService;
import org.json.JSONObject;

import java.io.*;
import java.net.Socket;

public class ChatRoomMessageController extends BaseController {

    private final ChatRoomMessageService chatRoomMessageService = new ChatRoomMessageService();

    public void handleSendMessage(JSONObject request, Socket socket) throws IOException {
        int senderId = request.getInt("sender_id");
        String message = request.getString("message");

        boolean success = chatRoomMessageService.sendMessage(senderId, message);
        if (success) {
            JSONObject response = createSuccessResponse("群聊消息发送成功");
            sendResponse(socket, response);
        } else {
            JSONObject response = createErrorResponse("消息发送失败");
            sendResponse(socket, response);
        }
    }

    private void sendResponse(Socket socket, JSONObject response) throws IOException {
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        out.println(response.toString());
    }
}
